<template>
  <el-dialog :title="textMap[dialogStatus]"
             :visible.sync="dialogFormVisible2"
             :close-on-click-modal="false"
             :close-on-press-escape="false"
             :fullscreen="false">
    <el-form ref="dataForm"
             :rules="rules"
             :model="formData"
             label-width="130px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="租户名"
                        prop="name">
            <el-input v-model="formData.name"
                      disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="描述"
                        prop="description">
            <el-input v-model="formData.description"
                      disabled />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="地址"
                        prop="address">
            <el-input v-model="formData.address" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="负责人"
                        prop="managerPerson">
            <el-input v-model="formData.managerPerson" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="负责人电话"
                        prop="managerMobile">
            <el-input v-model="formData.managerMobile" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row class="dialog-btn">
        <el-col :span="24">
          <el-form-item>
            <el-button type="info"
                       @click="dialogFormVisible2 = false"
                       size="mini">取消</el-button>
            <el-button type="primary"
                       size="mini"
                       @click="dialogStatus==='create'?createData():updateData()">保存</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </el-dialog>
</template>

<script>
import * as apiTenant from '@/api/tenant'

export default {
  name: 'UpdateTenant',
  props: ['textMap', 'dialogStatus', 'dialogFormVisible'],
  computed: {
    dialogFormVisible2: {
      get () {
        return this.dialogFormVisible
      },
      set (val) {
        // dialogFormVisible改变由父组件控制
        this.$emit('update:dialogFormVisible', val)
      }
    }
  },
  created () {
  },
  data () {
    return {
      formData: {
        id: undefined,
        name: undefined,
        description: undefined,
        address: undefined,
        managerPerson: undefined,
        managerMobile: undefined
      },
      rules: {
      }
    }
  },
  methods: {
    clearFormData () {//清除 this.formData数据
      Object.keys(this.formData).forEach(key => {
        this.formData[key] = undefined
      })
    },
    setCurrentRow (currentRowData) {// 修改 某条记录时 获取记录信息
      this.formData = Object.assign({}, currentRowData)
    },
    updateData () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const tempData = Object.assign({}, this.formData)
          apiTenant.update(tempData).then(() => {
            // 通知父组件刷新列表数据
            this.$emit('on-fresh-list')
            this.dialogFormVisible2 = false
            this.$notify({
              title: '成功',
              message: '修改成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    }
  }
}
</script>

<style scoped>
</style>
